
/**
 * @param {WebMail} oWebMail
 * @return {WebMailTemplateDataBuilder}
 */
WebMailTemplateDataBuilder = function (oWebMail)
{
	this.webmail = oWebMail;
	return this;
};

WebMailTemplateDataBuilder.prototype =
{
	/**
	 * @param {object} oMessage
	 * @return {object}
	 */
	buildMessage: function (oMessage)
	{
		var sListItemKey = this.webmail.generateHash({
			folder: oMessage.Folder, uid: oMessage.Uid
		});
						
		return $('<div class="message-item hide">')
			.attr('rel', sListItemKey)
			.attr('data-x-tabname', oMessage.Info.TabName)
			.pureJSTemplate({
				id   : 'template-message.html',
				data : {
					Subject: this.encodeHtml(oMessage.Info.Subject),
					From: this.encodeHtml(oMessage.Info.From),
					FromShort: this.encodeHtml(oMessage.Info.FromShort),
					Date: this.encodeHtml(oMessage.Info.Date),
					Size: oMessage.Info.Size,
					HasImages: oMessage.HasImages || false,
					HasPlain: 0 < (oMessage.Plain || '').length,
					HasHtml: 0 < (oMessage.Html || '').length,
					SaveLink: oMessage.Info.SaveLink,
					Attachments: oMessage.Info.Attachments || []
				}
		});
	},

	/**
	 * @param {object} oMessageHeader
	 * @return {object}
	 */
	buildMessageLine: function (oMessageHeader)
	{
		return $('<div>').pureJSTemplate({
			id   : 'template-message-line.html',
			data : {
				Uid: oMessageHeader.Uid,
				Subject: this.encodeHtml(oMessageHeader.Subject),
				From: this.encodeHtml(oMessageHeader.FromShort),
				Size: oMessageHeader.Size,
				Date: oMessageHeader.Date,
				HasAttachments: oMessageHeader.HasAttachments,
				IsFlagged: oMessageHeader.IsFlagged,
				IsUnread: oMessageHeader.IsUnread,
				IsAnswered: oMessageHeader.IsAnswered
			}
		});
	},

	buildMessageList: function (oList)
	{
		return $('<div class="list-item hide">')
			.attr('rel', this.webmail.generateHash({ folder: oList.Folder }))
			.attr('data-x-tabname', oList.TabName)
			.pureJSTemplate({
				id   : 'template-list.html',
				data : {}
		});
	},

	/**
	 * @param {array} aFolders
	 * @return {object}
	 */
	buildFolderList: function (aFolders)
	{
		var oAddFolderData, oData, iKey;
		oData = {List: []};
		for (iKey in aFolders)
		{
			oAddFolderData = {
				Rel: this.webmail.generateHash({folder: aFolders[iKey].FullName}),
				Name: aFolders[iKey].Name,
				SubFolders: '',
				NoSelect: aFolders[iKey].NoSelect
			};

			if (aFolders[iKey].SubFolders && 0 < aFolders[iKey].SubFolders.length)
			{
				oAddFolderData.SubFolders = this.buildFolderList(aFolders[iKey].SubFolders).html();
			}

			oData.List.push(oAddFolderData);
		}

		return $('<div>').pureJSTemplate({
		   id   : 'template-folders.html',
		   data : oData
		});
	},

	/**
	 * @param {string} sText
	 * @return {string}
	 */
	encodeHtml: function (sText)
	{
		return sText.replace(/&/g, "&amp;").replace(/</g, "&lt;")
			.replace(/>/g, "&gt;").replace(/"/g, '&quot;').replace(/'/g, '&#039;');
	}
}
